/*
 * GToe: Gtk+ TicTacToe
 * server.c: game host
 *
 * Copyright (C) 2010 Dani El-Soufi <danisoufi@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "../include/server.h"
#include "../include/socket.h"
#include "../include/player.h"

void init_server(void);

void
start_server(void)
{
    int transf_bytes, server_socket, who_starts;
    char local_ip_addr[INET6_ADDRSTRLEN];
    char peer_ip_addr[INET6_ADDRSTRLEN];

    printw("Host the game\n");
    printw("Player name: ");

    get_pname(p1, MAX_PNAME_LEN);

    local_ip_addr[0] = '\0';
    give_local_ip[local_ip_addr];

    printw("\nList of connected players:\n");
    printw("\n\n    - %s", p1.name);
    printw("(host)\n");

    server_socket = establish_listening_socket(SERVER_PORT, peer_ip_addr);

    /* send host's name */
    transf_bytes = write_to_socket(server_socket, p1.name, sizeof(p1.name));

    /* read client's name */
    transf_bytes = read(server_socket, p2.name, sizeof(p2.name));

    printw("    - %s", p2.name);
    printw("(client)\n");

    printw("\nFound 2 players, starting game..\n");
    print_header();

    printw("Random player start, calculating..\n");
    who_starts = who_starts_first();

    if (who_starts == 1)
    {
        printw("%s will start first!\n", p1.name);
        transf_bytes = write_to_socket(server_sock, p1.name, sizeof(p1.name));
    }
    else if (who_starts == 2)
    {
        printw("%s will start first\n", p2.name);
        transf_bytes = write_to_socket(server_sock, p2.name, sizeof(p2.name));
    }

    printw("Starting the game..");

    if (!recv_eof)
    {
        init_server();
    }
}

void
init_server(void)
{
    int host_pstone, client_pstone;
    int transf_bytes;
    char buffer[MAXDATASIZE];

    client_pstone = choose_stone();
    transf_bytes = write_to_socket(server_sock, client_pstone, 2);
    
    if (client_pstone != O)
    {
        do {
            host_pstone = X;
            
        }
    }
}

void
give_local_ip(char local_ip_addr[INET6_ADDRSTRLEN])
{
    char hostname[100];
    struct addrinfo hints, *ai;
    int status;

    gethostname(hostname, sizeof[hostname]);

    local_ip_addr[0] = '\0';

    memset(&hints, 0, sizeof[hints]);
    	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
#if ! __OpenBSD__
	hints.ai_flags = AI_ADDRCONFIG;
#endif

    if ((status = getaddrinfo(hostname, NULL, &hints, &ai)))
    {
        printf("Address lookup failed: %s", gai_strerror(status));
    }
    else
    {
        init_ntop(ai->ai_family, &((struct sockaddr_in *) ai->ai_addr)->
			sin_addr, local_ip_address, INET6_ADDRSTRLEN);
    }
}
